require("dplyr")

##. Set working directory
## to Dataverse folder

survey <- read.table("Harris_Data/Harris 2008 Election Survey, study no. 35172/Election_2008_Nov_99.tab", header = TRUE)

# pid
survey$pid <- c(1:nrow(survey))

# study 
survey$study <- "35172"

# study year (year)
survey$year <- 2008

# geographic data (urban)
table(survey$q105)
survey$urban <- dplyr::recode(survey$q105,
                       `1` = "Urban",
                       `2` = "Suburban",
                       `3` = "Rural")
table(survey$urban)

# geographic data (region)
table(survey$q104)
survey$region <- dplyr::recode(survey$q104,
                        `1` = "West",
                        `2` = "East",
                        `3` = "Midwest",
                        `4` = "South")
table(survey$region)

# respondent head of household (hh)
survey$hh <- NA

# increasing inequality (inequality)
table(survey$q876a2)
survey$inequality <- dplyr::recode(as.character(survey$q876a2),
                            `1` = "Feel",
                            `2` = "Don't Feel",
                            `-8` = "Not Sure",
                            `-9` = "Refused")
#survey$inequality[survey$inequality == "Refused"] <- NA
table(survey$inequality)

# inequality variable (inequality.variable)
survey$inequality.variable <- 1

# union (union.self)
survey$union.self <- NA
survey$union.other <- NA

# employment (employed)
survey$employed <- NA

# empl self
survey$employed.self <- NA

# occupation
survey$occupation <- NA

# occ self
survey$occupation.self <- NA

# household size (hhsize)
table(survey$q204)
survey$hhsize_over18 <- as.numeric(survey$q204)
survey$hhsize_over18[survey$hhsize_over18 < 0] <- NA
survey$hhsize_1317 <- as.numeric(survey$q1035)
survey$hhsize_1317[survey$hhsize_1317 < 0] <- NA
survey$hhsize_under13 <- as.numeric(survey$q1040)
survey$hhsize_under13[survey$hhsize_under13 < 0] <- NA
survey$hhsize <- rowSums(survey[, c("hhsize_over18",
                                    "hhsize_1317",
                                    "hhsize_under13")],
                         na.rm = TRUE)
survey$hhsize[survey$hhsize == 0] <- NA
table(survey$hhsize)

# education (educ)
table(survey$q216)
survey$educ <- dplyr::recode(survey$q216,
                      `1` = "Less than high school",
                      `2` = "Completed some high school",
                      `3` = "High school graduate",
                      `4` = "Some college",
                      `5` = "Associates degree",
                      `6` = "Bachelors degree",
                      `7` = "Some graduate school",
                      `8` = "Completed post graduate",
                      `-8` = "Not Sure",
                      `-9` = "Refused")
table(survey$educ)

# household income (income)
table(survey$q231)
table(survey$q233)
table(survey$q235)

## create special variable to indicate "don't know"/"refused" on initial question
survey$q233[survey$q231 < 0] <- -100
survey$q235[survey$q231 < 0] <- -100

## take the income data only for those making less than $50k
survey$income <- dplyr::recode(survey$q233,
                        `1` = "$0 to $14,999",
                        `2` = "$15,000 to $24,999",
                        `3` = "$25,000 to $34,999",
                        `4` = "$35,000 to $49,999",
                        `-8` = "Not sure",
                        `-9` = "Refused",
                        `-99` = "Over $50,000",
                        `-100` = "Disregard") # disregarding DK/refused
table(survey$income)

## combine with income data for those making more than $50k
## do this by only using the points where <$50k is NOT available
survey$income[survey$income == "Over $50,000"] <- survey$q235[survey$q233 == -99]
table(survey$income)

## recode >$50k data
survey$income <- dplyr::recode(survey$income,
                        `5` = "$50,000 to $74,999",
                        `6` = "$75,000 to $99,999",
                        `7` = "$100,000 to $124,999",
                        `8` = "$125,000 to $149,999",
                        `9` = "$150,000 to $199,999",
                        `10` = "$200,000 to $249,999",
                        `11` = "$250,000 or more",
                        `-8` = "Not sure",
                        `-9` = "Refused")
table(survey$income)

## get rid of initial data that was supposed to be disregarded
survey$income[survey$income == "Disregard"] <- NA
table(survey$income)

# age
table(survey$q1030)
survey$age <- as.character(survey$q1030)
survey$age[survey$age < 0] <- NA
table(survey$age)

# race
table(survey$q238)
survey$race1 <- dplyr::recode(survey$q238,
                      `1` = "White",
                      `2` = "Black",
                      `3` = "African-American",
                      `4` = "Asian or Pacific Islander",
                      `5` = "American Indian or Alaskan native",
                      `6` = "Mixed race",
                      `7` = "Some other race",
                      `-8` = "Not sure",
                      `-9` = "Refused")
survey$race2 <- dplyr::recode(survey$q236,
                              `1` = "Yes, hispanic",
                              `2` = "No, not hispanic",
                              `-9` = "Decline")
table(survey$race2)
survey$race <- ifelse(survey$race1 == "Not sure" |
                        survey$race1 == "Refused", 
                      "Not sure/refuse", ifelse(survey$race1 == "White",
                                                ifelse(survey$race2 == "Decline",
                                                       "Not sure/refuse",
                                                       ifelse(survey$race2 == "No, not hispanic",
                                                              "Non-Hispanic White", "Hispanic White")), "Non-white"))
table(survey$race1) ## 41 not sure/refused
table(survey$race2[survey$race1 == "White"])


# politics (party)
table(survey$q1500)
survey$party <- dplyr::recode(survey$q1500,
                       `1` = "Republican",
                       `2` = "Democrat",
                       `3` = "Independent",
                       `7` = "Other",
                       `-8` = "Not sure",
                       `-9` = "Refused")
table(survey$party)

# politics (ideology)
table(survey$q1520)
survey$ideology <- dplyr::recode(survey$q1520,
                          `1` = "Conservative",
                          `2` = "Moderate",
                          `3` = "Liberal",
                          `-8` = "Not sure",
                          `-9` = "Refused")
table(survey$ideology)

# gender
table(survey$q1020)
survey$gender <- dplyr::recode(survey$q1020,
                        `1` = "Male",
                        `2` = "Female")
table(survey$gender)

# religion
survey$religion <- NA

#factuals
survey$factual1 <- NA
survey$factual2 <- NA
survey$factual3 <- NA

## alienation index
survey$dontcare <- dplyr::recode(survey$q876a1,
                                 `1` = "Feel",
                                 `2` = "Don't Feel",
                                 `-8` = "Not Sure",
                                 `-9` = "Refused")
survey$dontcount <- dplyr::recode(survey$q876a3,
                                  `1` = "Feel",
                                  `2` = "Don't Feel",
                                  `-8` = "Not Sure",
                                  `-9` = "Refused")
survey$leftout <- dplyr::recode(survey$q876a4,
                                `1` = "Feel",
                                `2` = "Don't Feel",
                                `-8` = "Not Sure",
                                `-9` = "Refused")

## quesiton place
survey$question_place <- "before party"


# subset
survey_35172 <- survey[,c("pid", "study", "year", "urban", "region", "hh",
                          "inequality", "inequality.variable", "union.self", "union.other",
                          "employed", "employed.self", "occupation", "occupation.self", "hhsize", "educ", "income", 
                          "age", "race", "party", "ideology", "gender", "religion",
                          "factual1", "factual2", "factual3", "dontcare", "dontcount", "leftout",
                          "question_place")]


# save file
#saveRDS(survey_35172, file = "Harris_Data/survey_35172.rds")
